﻿'*******************************************************************************************
'* Copyright Brügmann Software GmbH, Papenburg
'* Author: Björn Kremer
'* Contact: codeplex<at>patorg.de
'* 
'* VB.CMIS is a VB.NET implementation of the Content Management Interoperability Services (CMIS) standard
'*
'* This file is part of VB.CMIS.
'* 
'* VB.CMIS is free software: you can redistribute it and/or modify
'* it under the terms of the GNU Lesser General Public License as published by
'* the Free Software Foundation, either version 3 of the License, or
'* (at your option) any later version.
'* 
'* VB.CMIS is distributed in the hope that it will be useful,
'* but WITHOUT ANY WARRANTY; without even the implied warranty of
'* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'* GNU Lesser General Public License for more details.
'* 
'* You should have received a copy of the GNU Lesser General Public License
'* along with VB.CMIS. If not, see <http://www.gnu.org/licenses/>.
'*******************************************************************************************
Imports CMISObjectModel
Public Class CMISSampleData
   Public Property Id As String = Guid.NewGuid.ToString
   Public Property Title As String
   Public Property Value1 As String
   Public Property Value2 As String
   Public Property Value3 As Decimal
   Public Property ContentStream As Byte()
   Public Property Mimetype As String
   Public Property FileName As String
   Public Property CreationDate As DateTime = Now
   Public Property CreatedBy As String = "test"
   Public Property Path As String = ""
   Public Property SourceId As String
   Public Property TargetId As String
   Public Property Relationships As New Dictionary(Of String, CMISSampleData)

   Private _typeData As TypeData
   Public ReadOnly Property TypeData As TypeData
      Get
         Return _typeData
      End Get
   End Property
   Public Sub New(id As String, typeData As TypeData)
      AddHandler FindParentEvent, AddressOf FindParentHandler
      AddHandler RemoveChildEvent, AddressOf RemoveChildHandler
      Me.Id = id
      _typeData = typeData
   End Sub

   Public ReadOnly Property Author As String
      Get
         Return CreatedBy & "@example.org"
      End Get
   End Property
   
   Private Property Children As New Dictionary(Of String, CMISSampleData)
   Public Sub AddChild(id As String, data As CMISSampleData)
      Children.Add(id, data)
   End Sub
   Public Sub RemoveChildEntry(id As String)
      Children.Remove(id)
   End Sub
   Public ReadOnly Property ChildrenCount As Integer
      Get
         If Children Is Nothing Then
            Return -1
         End If
         Return Children.Count
      End Get
   End Property
   Public ReadOnly Property SortedChildren As List(Of CMISSampleData)
      Get
         Return Children.Values.OrderBy(Function(l) l.Title).ToList
      End Get
   End Property

   Private Shared Event FindParentEvent(sender As Object, e As FindParentEventArgs(Of CMISSampleData))
   Public Shared Function FindParent(pkey As String) As List(Of CMISSampleData)
      Dim tmpArgs As New FindParentEventArgs(Of CMISSampleData)(pkey)
      RaiseEvent FindParentEvent(Nothing, tmpArgs)
      Return tmpArgs.ResultData
   End Function

  

   Private Sub FindParentHandler(sender As Object, e As FindParentEventArgs(Of CMISSampleData))
      If Children.ContainsKey(e.SearchPkey) Then
         e.ResultData.Add(Me)
      End If
   End Sub

   Private Shared Event RemoveChildEvent(sender As Object, e As FindParentEventArgs(Of CMISSampleData))
   Public Shared Sub RemoveChild(pkey As String)
      Dim tmpArgs As New FindParentEventArgs(Of CMISSampleData)(pkey)
      RaiseEvent RemoveChildEvent(Nothing, tmpArgs)
   End Sub



   Private Sub RemoveChildHandler(sender As Object, e As FindParentEventArgs(Of CMISSampleData))
      If Children.ContainsKey(e.SearchPkey) Then
         Children.Remove(e.SearchPkey)
      End If
      If Relationships.ContainsKey(e.SearchPkey) Then
         Relationships.Remove(e.SearchPkey)
      End If
   End Sub

   Public Overrides Function ToString() As String
      Return Me.Id & " - " & MyBase.ToString()
   End Function

End Class
